/**
 * 心跳
 */

// env
require('../env');

const fs = require('fs');
const spawn = require('child_process').spawn;
const logger = require('../app/services/logger');

module.exports = {
    PING_INTERVAL: 30 * 1000,

    start() {
        this.startAgentx();

        this.checkTimer = setInterval(this.check, this.PING_INTERVAL);
    },
    stop() {
        clearInterval(this.checkTimer);
    },

    startAgentx() {
        const cfgPath = './agentx-conf.json';

        // Only start with production
        if(
            process.env.APP_STAGE !== 'production' &&
            fs.existsSync(cfgPath)
        ) {
            return null;
        }

        // return spawn('agenthub', [cfgPath], {
        //     stdio: 'inherit',
        //     env: Object.assign({}, process.env, {
        //         ENABLE_NODE_LOG: 'NO'
        //     })
        // });
    },

    check() {
        // @TODO: 检查应用状态，网络状态，上报
        logger.info('Ping:', {
            status: 'success'
        });
    }
};

// Run as a server
if(require.main === module) {
    module.exports.start();
}
